Repository navigation
fix: keep a call body that moved to the next line after multi-line arrow parameters there when formatting again - #897
Merged
Conversation
…row parameters there when formatting again
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Transparency note: This PR was prepared with AI assistance.
Fixes #896.
Makes an arrow function format the same way on every pass when its parameters span several lines and its body is a call whose last argument spans several lines.
Problem
With
indentWidth: 4and the defaultlineWidth, 0.97.0 never settles on this input, and the CLI bails withFormatting not stable:The first pass moves the call to the line after
) =>. The second pass puts it back on the arrow's line, and so on. #896 has the output of each pass. 0.96.1 leaves the input unchanged. WithindentWidth: 2, 0.97.0 moves the call to the next line and keeps it there.Cause
#861 added
is_hugging_call_bodyingen_arrow_func_expr. When the call starts on the arrow's line and its last argument starts on that line and spans more lines, the separator after=>isSignal::SpaceOrNewLine. Here the text before the last argument doesn't fit, so the call moves to the next line.On the next pass the call is no longer on the arrow's line, so the body takes the older path:
if_above_width_or(indent_width, ..., " ").) =>ends at column 4, which is not past the indent width of 4, so that path prints a space and the call goes back on the arrow's line. WithindentWidth: 2the column is past the indent width, the body spans several lines, and the older path keeps the newline. That is why onlyindentWidth: 4alternates.Change
In the older path, when the column is not past the indent width and the body is a call that hugs its last argument, the separator is now
Signal::SpaceOrNewLineinstead of a space. That is the separator #861's branch uses, so the result no longer depends on whether the input had the call on the arrow's line:) =>moves to the next line and stays there.) =>is put on the arrow's line, as 0.97.0 does today.The call-shape check moves out of
is_hugging_call_bodyintois_call_hugging_last_arg, so both paths use it. Bodies that aren't such a call still get a space, and headers past the indent width are unchanged.Tests
tests/specs/issues/issue0107.txt, next to fix: keep an arrow function's call or template literal body on the same line as the arrow when it hugs its last argument or is on multiple lines #861's specs:) => foo({) stays on the arrow's line, and the same call on the next line moves back to it.tests/specs/issues/issue0107_02.txt(new): the input above withindentWidth: 2keeps 0.97.0's output.issue0107.txtandexternal_formatter/html.txt.Real code
I built the Wasm plugin and ran it with CLI 0.64.3 and a config with
lineWidth: 180andindentWidth: 4:Formatting not stable. The build changes oneit(...)body in it: it moves to the line after) =>.dprint fmtchanges nothing, anddprint checkpasses.